import '../../public.dart';

const Color defaultColor = Colors.grey;
const Color defaultActivedColor = Colors.red;

class BottomBar extends StatelessWidget {
  final List<RootTabPage> list;
  final int currentIndex;
  final Function onSelect;
  const BottomBar(
    this.list, {
    super.key,
    required this.onSelect,
    required this.currentIndex,
  });

  @override
  Widget build(BuildContext context) {
    return ProviderWidget(
      builder: (conetxt, model, child) {
        return BrnBottomTabBar(
          fixedColor: Colors.blue,
          currentIndex: currentIndex,
          onTap: (index) {
            // 如果该 tab 需要登录权限
            if (list[index].autorized && !model.isLogin) {
              // 未登录则跳转登录页
              RouterHelper.navigator.pushNamed(Routers.login);
            } else {
              // 已登录或无权限要求，直接切换
              onSelect(index);
            }
          },
          badgeColor: Colors.red,
          items: List.generate(
            list.length,
            (index) => BrnBottomTabBarItem(
              backgroundColor: Colors.red,
              title: Text(
                list[index].name ?? '',
                style: TextStyle(color: list[index].color),
              ),
              icon: list[index].icon,
              activeIcon: list[index].activedIcon,
              selectedTextStyle: TextStyle(color: list[index].activedColor),
            ),
          ),
        );
      },
      model: Provider.of<UserModel>(context),
    );
  }
}

class RootTabPage {
  Widget page;
  String? name;
  Widget activedIcon;
  Widget icon;
  Color color;
  Color activedColor;
  bool autorized;
  RootTabPage({
    required this.page,
    this.name,
    Widget? activedIcon,
    required this.icon,
    this.color = Colors.grey,
    this.activedColor = Colors.red,
    this.autorized = false,
  }) : activedIcon = activedIcon ?? icon;
}
